package com.hnguigu.health.houduan.lq.controller_lq;

import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.hnguigu.health.houduan.lcj.pojo_lcj.TOrder_lcj;
import com.hnguigu.health.houduan.lq.service_lq.*;
import com.hnguigu.health.houduan.xsg.service_xsg.t_userService;
import com.hnguigu.health.pojo.*;
import com.hnguigu.health.utils.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/lqHealth")
public class LqHealthController {
    @Autowired(required = false)
    TBodyTypeService tBodyTypeService;
    @Autowired(required = false)
    TMentalityResultService tMentalityResultService;
    @Autowired(required = false)
    TPhysicalTableService tPhysicalTableService;
    @Autowired(required = false)
    TUserConstitutionTableService tUserConstitutionTableService;
    @Autowired(required = false)
    TPhysicalEvaluationTableService tPhysicalEvaluationTableService;
    @Autowired(required = false)
    TUserPsychologicalEvaluationService tUserPsychologicalEvaluationService;
    @Autowired(required = false)
    TInterposeClassifyService tInterposeClassifyService;
    @Autowired(required = false)
    THealthmgrService tHealthmgrService;
    @Autowired(required = false)
    TMemberService tMemberService;
    @Autowired(required = false)
    TMealsService tMealsService;
    @Autowired(required = false)
    TSportsItemLibraryService tSportsItemLibraryService;
    @Autowired(required = false)
    TTemplatemanageService tTemplatemanageService;
    @Autowired(required = false)
    TInterventionprogrammesService tInterventionprogrammesService;
    @Autowired(required = false)
    TFollowupjournalService tFollowupjournalService;
    @Autowired(required = false)
    TOrderService ts;
    @Autowired(required = false)
    t_userService TuserMapper;
    //查询预约人员
    @PostMapping("/tOrderBytmerber")
    @ResponseBody
    public TOrder tOrder_lcjBytmerber(@RequestBody Map<String,String> map){
        String tmerber = map.get("tmerber");
        List<TOrder> tOrders = ts.tOrderBytmerber(tmerber);
        if(tOrders.size()>0){
            return tOrders.get(0);
        }
        return null;
    }
    //分页
    @PostMapping("/selectAllTbodyType")
    @ResponseBody
    public PageInfo<TBodyType> selectAllTbodyType(@RequestBody Map<String,String> map){
        String page = map.get("page");
        String num = map.get("num");
        page=page==null?"1":page;
        String content = map.get("content");
        content=content.equals("")?null:content;
        return tBodyTypeService.selectAllType(content,Integer.parseInt(page),Integer.parseInt(num));
    }
    //心里测评分页
    @PostMapping("/selectAlltMentalityResult")
    @ResponseBody
    public PageInfo<TMentalityResult> selectAlltMentalityResult(@RequestBody Map<String,String> map){
        String page = map.get("page");
        String num = map.get("num");
        page=page==null?"1":page;
        String content = map.get("content");
        content=content.equals("")?null:content;
        return tMentalityResultService.selectAllTmenResult(content,Integer.parseInt(page),Integer.parseInt(num));
    }
    //所有的表以及表下的题目
    @GetMapping("/selectAllTQuestion")
    @ResponseBody
    public List<TPhysicalTable> selectAllTQuestion(){
        return  tPhysicalTableService.selectAllTQuestion();

    }
    @GetMapping("/selectAllTPhysicalEvaluation")
    @ResponseBody
    public List<TPhysicalEvaluationTable> selectAllTPhysicalEvaluation(){
        return  tPhysicalEvaluationTableService.selectAllTPhysicalEvaluation();
    }
    //添加数据 修改与添加复用高 可以合并抽取
    @PostMapping("/adduserconstitution")
    @ResponseBody
    public int adduserconstitution(@RequestBody Map<String,String> map){
        String data = map.get("user");
        JSONObject jsonObject = JSONObject.fromObject(data);
        String allAnswer = (map.get("allAnswer"));
        JSONArray jsonArray = JSONArray.fromObject(allAnswer);
        //获取传输的bean信息 user
        UserPojo user=(UserPojo)JSONObject.toBean(JSONObject.fromObject(data),UserPojo.class);
        Date date = new Date();
        Map<String,Class<RequseionallAnswer>> map1=new HashMap<String,Class<RequseionallAnswer>>();
        map1.put("arr",RequseionallAnswer.class);
        //获取传输的数组 并进行递归翻译成list集合
        List<Userconstitution> uus= (List)JSONArray.toCollection(JSONArray.fromObject(allAnswer),Userconstitution.class);
        //判断这条数据是否已经存在
        TBodyType tBodyType = tBodyTypeService.selectBytMerber(user.getToOrderno());
        if(tBodyType==null){
            TBodyType tBodyTypeb=new TBodyType();
            int[] array=new int[3];
            for (int i=0;i<jsonArray.size();i++) {
                Userconstitution o1 = (Userconstitution) JSONObject.toBean((JSONObject) jsonArray.get(i), Userconstitution.class, map1);
                UserContionXz contionXz=new UserContionXz();
                contionXz.setUserPojo(user);
                int compute=0;
                contionXz.setUserconstitution(o1);
                for (RequseionallAnswer requseionallAnswer : contionXz.getUserconstitution().getArr()) {
                    compute+=requseionallAnswer.getVal();
                }
                //计算分数是否为有病
                array[i]=compute/contionXz.getUserconstitution().getArr().size();
                tUserConstitutionTableService.addUserConstitution(contionXz);
            }
            //添加tbody表内容
            tBodyTypeb.settYangQuality(array[0]);
            tBodyTypeb.settYinDeficiency(array[1]);
            tBodyTypeb.settQiAsthenia(array[2]);
            tBodyTypeb.setCreatetime(date);
            tBodyTypeb.settMemberId(user.getToOrderno());
            tBodyTypeb.settBodyName(user.getTusername());
            int i =tBodyTypeService.insertSelective(tBodyTypeb);
            if(i>0){
                return 1;
          }
        }
        return 0;
    }
    @PostMapping("/addUserEvaluationConstitution")
    @ResponseBody
    public int addUserEvaluationConstitution(@RequestBody Map<String,String> map){
        String data = map.get("user");
        JSONObject jsonObject = JSONObject.fromObject(data);
        String allAnswer = (map.get("allAnswer"));
        JSONArray jsonArray = JSONArray.fromObject(allAnswer);
        //获取传输的bean信息 user
        UserPojo user=(UserPojo)JSONObject.toBean(JSONObject.fromObject(data),UserPojo.class);
        Date date = new Date();
        Map<String,Class<RequseionallAnswer>> map1=new HashMap<String,Class<RequseionallAnswer>>();
        map1.put("arr",RequseionallAnswer.class);
        //获取传输的数组 并进行递归翻译成list集合
        List<Userconstitution> uus= (List)JSONArray.toCollection(JSONArray.fromObject(allAnswer),Userconstitution.class);

        TMentalityResult tMentalityResult = tMentalityResultService.selectBytmerber(user.getToOrderno());
        if(tMentalityResult==null){
            TMentalityResult TMentality=new TMentalityResult();
            int[] array=new int[3];
            for (int i=0;i<jsonArray.size();i++) {
                Userconstitution o1 = (Userconstitution) JSONObject.toBean((JSONObject) jsonArray.get(i), Userconstitution.class, map1);
                UserContionXz contionXz=new UserContionXz();
                contionXz.setUserPojo(user);
                int compute=0;
                contionXz.setUserconstitution(o1);
                for (RequseionallAnswer requseionallAnswer : contionXz.getUserconstitution().getArr()) {
                    compute+=requseionallAnswer.getVal();
                }
                //计算分数是否为有病
                array[i]=compute/contionXz.getUserconstitution().getArr().size();
                tUserPsychologicalEvaluationService.addUserEvaluationConstitution(contionXz);
            }
            //添加tbody表内容
            TMentality.settOcd(array[0]);
            TMentality.settAnxiety(array[1]);
            TMentality.settTerror(array[2]);
            TMentality.setCreatetime(date);
            TMentality.settMemberId(user.getToOrderno());
            TMentality.settMemberName(user.getTusername());
            tMentalityResultService.insertSelective(TMentality);
        }else{
            return -1;
        }
        TBodyType tBodyType = tBodyTypeService.selectBytMerber(user.getToOrderno());
        TInterposeClassify tInterposeClassify1 = tInterposeClassifyService.selectByTmerber(user.getToOrderno());
        TMentalityResult tMentalityResult2 = tMentalityResultService.selectBytmerber(user.getToOrderno());

        //两次评测添加含有之后进行人群分类表的添加
        if (tBodyType!=null && tMentalityResult2!=null && tInterposeClassify1==null){
            TInterposeClassify tInterposeClassify=new TInterposeClassify();
            tInterposeClassify.setClassification("系统分类");
            tInterposeClassify.setTMemberId(user.getToOrderno());
            tInterposeClassify.setStatus(0);
            tInterposeClassify.setAge(user.getTuserage());
            tInterposeClassify.setSex(user.getTusersex());
            tInterposeClassify.setName(user.getTusername());
            tInterposeClassify.setCreatetime(date);
            tInterposeClassify.setTiOrderno(user.getTmerberid());
            int tBodyCount=tBodyType.gettQiAsthenia()+tBodyType.gettYangQuality()+tBodyType.gettYinDeficiency();
            int tMentalityCount=tMentalityResult2.gettOcd()+tMentalityResult2.gettAnxiety()+tMentalityResult2.gettTerror();
            int tInterposeCount=tMentalityCount+tBodyCount;
            if(tInterposeCount>15){
                tInterposeClassify.setPeopleClassify("高血压");
            }else{
                tInterposeClassify.setPeopleClassify("低血压");
            }
            tInterposeClassifyService.insertSelective(tInterposeClassify);
        }
        if(tMentalityResult2!=null && tBodyType==null){
            return 2;
        }
        if(tBodyType!=null && tMentalityResult2!=null){
            return 1;
        }
        return 0;
    }
    //查询用户体质选择最后一条获取姓名 sex age
    @PostMapping("/tcLastBean")
    @ResponseBody
    public TUserConstitutionTable tcLastBean(@RequestBody Map<String,String> map){
        return tUserConstitutionTableService.tcbyTmerberLast(map.get("tMemberId"));
    }
    //查询用户心理选择最后一条获取姓名 sex age
    @PostMapping("/tpLastBean")
    @ResponseBody
    public TUserPsychologicalEvaluation tpLastBean(@RequestBody Map<String,String> map){
        return tUserPsychologicalEvaluationService.tpbyTmerberLast(map.get("tMemberId"));
    }
    //详情
    @PostMapping("/selectAllTUserConstitution")
    @ResponseBody
    public Map<Integer,List<RequseionallAnswer>> selectAllTUserConstitution(@RequestBody Map<String,String> map){
        return tUserConstitutionTableService.selectAllTUserConstitution(map.get("tMemberId"));
    }
    @PostMapping("/selectAllTUserEvaluationConstitution")
    @ResponseBody
    public Map<Integer,List<RequseionallAnswer>> selectAllTUserEvaluationConstitution(@RequestBody Map<String,String> map){
        return tUserPsychologicalEvaluationService.selectAllTUserEvaluationConstitution(map.get("tMemberId"));
    }
    @PostMapping("/updateuserconstitution")
    @ResponseBody
    public int updateuserconstitution(@RequestBody Map<String,String> map){
        String data = map.get("user");
        String updateAllSwer = (map.get("updateAllSwer"));
        JSONArray jsonArray = JSONArray.fromObject(updateAllSwer);
        UserPojo user=(UserPojo)JSONObject.toBean(JSONObject.fromObject(data),UserPojo.class);
        //先全删了后批量添加
        int i1 = tUserConstitutionTableService.deleteByTmerber(user.getTmerberid());
        if(i1>0){
            Date date = new Date();
            Map<String,Class<RequseionallAnswer>> map1=new HashMap<String,Class<RequseionallAnswer>>();
            map1.put("arr",RequseionallAnswer.class);
            TBodyType tBodyTypeb=new TBodyType();
            int[] array=new int[3];
            for (int i=0;i<jsonArray.size();i++) {
                Userconstitution o1 = (Userconstitution) JSONObject.toBean((JSONObject) jsonArray.get(i), Userconstitution.class, map1);
                UserContionXz contionXz=new UserContionXz();
                contionXz.setUserPojo(user);
                int compute=0;
                contionXz.setUserconstitution(o1);
                System.out.println(o1);
                for (RequseionallAnswer requseionallAnswer : contionXz.getUserconstitution().getArr()) {
                    compute+=requseionallAnswer.getVal();
                }
                array[i]=compute/contionXz.getUserconstitution().getArr().size();
                tUserConstitutionTableService.addUserConstitution(contionXz);
            }
            tBodyTypeb.settYangQuality(array[0]);
            tBodyTypeb.settYinDeficiency(array[1]);
            tBodyTypeb.settQiAsthenia(array[2]);
            tBodyTypeb.setUpdatetime(date);
            tBodyTypeb.settMemberId(user.getTmerberid());
            //修改tbody表
            int i =tBodyTypeService.updateByPrimaryKeySelective(tBodyTypeb);
            if(i>0){
                return 1;
            }
        }
        return 0;
    }
    @PostMapping("/updateUserEvaluationConstitution")
    @ResponseBody
    public int updateUserEvaluationConstitution(@RequestBody Map<String,String> map){
        String data = map.get("user");
        String updateAllSwer = (map.get("updateAllSwer"));
        JSONArray jsonArray = JSONArray.fromObject(updateAllSwer);
        UserPojo user=(UserPojo)JSONObject.toBean(JSONObject.fromObject(data),UserPojo.class);
        //先全删了后批量添加
        int i1 = tUserPsychologicalEvaluationService.deleteByTmerber(user.getTmerberid());
        if(i1>0){
            Date date = new Date();
            Map<String,Class<RequseionallAnswer>> map1=new HashMap<String,Class<RequseionallAnswer>>();
            map1.put("arr",RequseionallAnswer.class);
            TMentalityResult TMentality=new TMentalityResult();
            int[] array=new int[3];
            for (int i=0;i<jsonArray.size();i++) {
                Userconstitution o1 = (Userconstitution) JSONObject.toBean((JSONObject) jsonArray.get(i), Userconstitution.class, map1);
                UserContionXz contionXz=new UserContionXz();
                contionXz.setUserPojo(user);
                int compute=0;
                contionXz.setUserconstitution(o1);
                System.out.println(o1);
                for (RequseionallAnswer requseionallAnswer : contionXz.getUserconstitution().getArr()) {
                    compute+=requseionallAnswer.getVal();
                }
                array[i]=compute/contionXz.getUserconstitution().getArr().size();
                tUserPsychologicalEvaluationService.addUserEvaluationConstitution(contionXz);
            }
            TMentality.settOcd(array[0]);
            TMentality.settAnxiety(array[1]);
            TMentality.settTerror(array[2]);
            TMentality.setUpdatetime(date);
            TMentality.settMemberId(user.getTmerberid());
            TMentality.settMemberName(user.getTusername());
            int i =tMentalityResultService.updateByPrimaryKeySelective(TMentality);
            if(i>0){
                return 1;
            }
        }
        return 0;
    }
    //查询所有完成评测会员信息
    @PostMapping("/selectAlltinterpose")
    @ResponseBody
    public PageInfo<TInterposeClassify> selectAlltinterpose(@RequestBody Map<String,String> map){
        String page = map.get("page");
        String num = map.get("num");
        page=page==null?"1":page;
        String content = map.get("content");
        content=content.equals("")?null:content;
        return tInterposeClassifyService.selectAllTInter(Integer.parseInt(page),Integer.parseInt(num),content);
    }
    //判断需要制定方案的会员是否已经被健康管理师分类人群
    @PostMapping("/chaTinterpose")
    @ResponseBody
   public int selectByTmerber(@RequestBody Map<String,String> map){
        String tmerber = map.get("tmerber");
        TInterposeClassify tInterposeClassify = tInterposeClassifyService.selectByTmerber(tmerber);
        if(tInterposeClassify!=null){
            if(tInterposeClassify.getStatus()!=0){
                return 1;
            }
        }
        return 0;
    }
    //人群分类查询本会员编号做的两次评测
    @PostMapping("/selecttMentalityResultBytmerBer")
    @ResponseBody
    public TMenTailTbody selecttMentalityResultBytmerBer(@RequestBody Map<String,String> map){
        TMenTailTbody tMenTailTbody=new TMenTailTbody();
        String tmerber = map.get("tMemberId");
        TBodyType tBodyType = tBodyTypeService.selectBytMerber(tmerber);
        TMentalityResult tMentalityResult = tMentalityResultService.selectBytmerber(tmerber);
        if(tBodyType!=null&&tMentalityResult!=null){
            tMenTailTbody.setTMentalityResult(tMentalityResult);
            tMenTailTbody.setTBodyType(tBodyType);
            return tMenTailTbody;
        }
     return null;
    }
    //查询所有的健康管理师
    @GetMapping("/selectAllTHealthmgr")
    @ResponseBody
    public List<THealthmgr> selectAllTHealthmgr(){
        return tHealthmgrService.selectAllTHealthmgr();
    }
    //修改t_interpose_classify表中的健康管理师
    @PostMapping("/updateTinterposeClassifyByUdr")
    @ResponseBody
    public int updateTinterposeClassifyByUdr(@RequestBody Map<String,String> map){
        String tMemberId = map.get("tMemberId");
        String value = map.get("value");
        String radio = map.get("radio");
        THealthmgr tHealthmgr = tHealthmgrService.selectAllTHealthmgrByhealthMgrId(Integer.parseInt(value));
        TInterposeClassify tInterposeClassify=new TInterposeClassify();
        tInterposeClassify.setTiOrderno(tMemberId);
        tInterposeClassify.setUdr(tHealthmgr.getHealthMgrName());
        tInterposeClassify.setStatus(1);
        tInterposeClassify.setUpdatetime(new Date());
        tInterposeClassify.setUdrId(Integer.parseInt(value));
        tInterposeClassify.setPeopleClassify(radio);
        int update = tInterposeClassifyService.updateByPrimaryKeySelective(tInterposeClassify);
        if(update>0){
            return 1;
        }
        return 0;
    }
    //查询所有的会员表信息
    @PostMapping("/selectAllTmerber")
    @ResponseBody
    public PageInfo<TMember> selectAllTmerber(@RequestBody Map<String,String> map){
        String page = map.get("page");
        String num = map.get("num");
        page=page==null?"1":page;
        String content = map.get("content");
        content=content.equals("")?null:content;
        return tMemberService.selectAllTmerber(Integer.parseInt(page),Integer.parseInt(num),content);
    }
    //查询所有模板 （包含已禁止的模板）
    @PostMapping("/selectAllTTemplatemanage")
    @ResponseBody
    public PageInfo<TTemplatemanage> selectAllTTemplatemanage(@RequestBody Map<String,String> map){
        String page = map.get("page");
        String num = map.get("num");
        page=page==null?"1":page;
        String content = map.get("content");
        content=content.equals("")?null:content;
        return tTemplatemanageService.selectAllTTemplatemanage(Integer.parseInt(page),Integer.parseInt(num),content);
    }
    //添加可用模板
    @PostMapping("/addTemplatemanage")
    @ResponseBody
    public int addTemplatemanage(@RequestBody Map<String,String> map){
        TTemplatemanage lastbean = tTemplatemanageService.Lastbean();
        String tTemplateid = lastbean.getTTemplateid();
        String newTmid="";
        String substring="";
        if(lastbean==null){
            newTmid="MB001";
        }else{
            substring = tTemplateid.substring(2, tTemplateid.length());
        }

        if(Integer.parseInt(substring)<10){
           newTmid= "MB00"+(Integer.parseInt(substring)+1);
        }
        if(Integer.parseInt(substring)>=10){
            newTmid= "MB0"+(Integer.parseInt(substring)+1);
        }
        if(Integer.parseInt(substring)>=100){
            newTmid= "MB"+(Integer.parseInt(substring)+1);
        }
        String t_indeit_scode = map.get("t_indeit_scode");
        String t_inmoving_code = map.get("t_inmoving_code");
        String title = map.get("title");
        String texta = map.get("texta");
        String adaptation = map.get("adaptation");
        String keywords=map.get("keywords");
        TTemplatemanage tTemplatemanage=new TTemplatemanage();
        tTemplatemanage.setKeywords(keywords);
        tTemplatemanage.setTForpeople(adaptation);
        tTemplatemanage.setStatus(0);
        tTemplatemanage.setTTarget(texta);
        tTemplatemanage.setTTitle(title);
        tTemplatemanage.setTTemplateid(newTmid);
        System.out.println(map +"====="+newTmid);
        int i1 = tTemplatemanageService.insertSelective(tTemplatemanage);
        if(i1>0){
            JSONArray jsonArray = JSONArray.fromObject(t_indeit_scode);
            String[] arr = new String[jsonArray.size()];
            for (int i = 0; i < jsonArray.size(); i++) {
                arr[i] = jsonArray.getString(i);
            }

            JSONArray jsonArray2 = JSONArray.fromObject(t_inmoving_code);
            String[] arr2 = new String[jsonArray2.size()];
            for (int i = 0; i < jsonArray2.size(); i++) {
                arr2[i] = jsonArray2.getString(i);
            }
            forManage forManage=new forManage();
            forManage.setTemangid(newTmid);
            forManage.setTindeitscodes(arr);
            forManage.setTinmovingcodes(arr2);
            int i = tTemplatemanageService.forInsertTd(forManage);
            int i2 = tTemplatemanageService.forInsertTm(forManage);
            if(i>0&&i2>0){
                return 1;
            }
        }
        return 0;
    }
    //查询所有的模板（只启用）
    @GetMapping("/AllTTemplatemanage")
    @ResponseBody
    public List<TTemplatemanage> AllTTemplatemanage(){
        return tTemplatemanageService.AllTTemplatemanage();
    }
    //删除人群分类
    @PostMapping("/deleteClassIf")
    @ResponseBody
    public int deleteClassIf(@RequestBody Map<String,String> map){
        int tmerber = tInterposeClassifyService.deleteByTermer(map.get("tmerber"));
        if(tmerber>0){
            return 1;
        }
        return 0;
    }
    //查看用户是否已经制定了方案
    @PostMapping("/chaInterventionprogrammes")
    @ResponseBody
    public int chaInterventionprogrammes(@RequestBody Map<String,String> map){
        String tmerber = map.get("tmerber");
        List<TInterventionprogrammestable> inquireabouttmetber = tInterventionprogrammesService.inquireabouttmetber(tmerber);
        if(inquireabouttmetber.size()!=0){
            return 1;
        }
        return 0;
    }
    //添加用户的制定方案
    @PostMapping("/insertInterventionprogrammes")
    @ResponseBody
    public int InsertInterventionprogrammes(@RequestBody Map<String,String> map) throws ParseException {
        String t_manage_templateId = map.get("t_Manage_templateId");
        String t_indeit_scode = map.get("t_indeit_scode");
        String t_inmoving_code = map.get("t_inmoving_code");
        String createDate = map.get("createDate");
        String endDate = map.get("endDate");
        String tmerber = map.get("tmerber");
        String name=map.get("name");
        Date parse = new SimpleDateFormat("yyyy-MM-dd").parse(createDate);
        Date parse1 = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);
        List<TInterventionprogrammestable> inquireabouttmetber = tInterventionprogrammesService.inquireabouttmetber(tmerber);
        if(inquireabouttmetber.size()==0){
            if(t_manage_templateId!=null){
                TInterventionprogrammestable tInterventionprogrammestable=new TInterventionprogrammestable();
                tInterventionprogrammestable.setTInterveneMerber(tmerber);
                tInterventionprogrammestable.setCreatedate(parse);
                tInterventionprogrammestable.setEnddate(parse1);
                tInterventionprogrammestable.setTintername(name);
                tInterventionprogrammestable.setExecutionstatus(0);
                tInterventionprogrammestable.setTManageTemplateid(t_manage_templateId);
                int i = tInterventionprogrammesService.insertSelective(tInterventionprogrammestable);
                TFollowupjournal tFollowupjournal=new TFollowupjournal();
                tFollowupjournal.setTFollowname(name);
                tFollowupjournal.setTFollowtmerber(tmerber);
                tFollowupjournal.setTfinish(0);
                tFollowupjournal.setTResult(0);
                tFollowupjournal.setTFollowdate(parse);
                tFollowupjournalService.insertSelective(tFollowupjournal);
                if(i>0){
                    return 1;
                }
            }else {
                JSONArray jsonArray = JSONArray.fromObject(t_indeit_scode);
                String[] arr=new String[jsonArray.size()];
                for(int i=0;i<jsonArray.size();i++){
                    arr[i]=jsonArray.getString(i);
                }

                JSONArray jsonArray2 = JSONArray.fromObject(t_inmoving_code);
                String[] arr2=new String[jsonArray2.size()];
                for(int i=0;i<jsonArray2.size();i++){
                    arr2[i]=jsonArray2.getString(i);
                }
                forInterventprogrammes forInterventprogrammes=new forInterventprogrammes();
                forInterventprogrammes.setTindeitscodes(arr);
                forInterventprogrammes.setTmerber(tmerber);
                forInterventprogrammes.setTinmovingcodes(arr2);
                forInterventprogrammes.setStauts(0);
                forInterventprogrammes.setName(name);
                forInterventprogrammes.setCreateDate(parse);
                forInterventprogrammes.setEndDate(parse1);
                int i = tInterventionprogrammesService.forInsert(forInterventprogrammes);
                TFollowupjournal tFollowupjournal=new TFollowupjournal();
                tFollowupjournal.setTFollowname(name);
                tFollowupjournal.setTFollowtmerber(tmerber);
                tFollowupjournal.setTfinish(0);
                tFollowupjournal.setTResult(0);
                tFollowupjournal.setTFollowdate(parse);
                //初始化添加一条随访日记
                tFollowupjournalService.insertSelective(tFollowupjournal);
                if(i>0){
                    return 1;
                }
            }
        }else{
            if(inquireabouttmetber.get(0).getExecutionstatus()>1){
                return 2;
            }
        }
        return -1;
    }
    //修改给用户制定得原本方案
    @PostMapping("/updateInterventionprogrammes")
    @ResponseBody
    public int updateInterventionprogrammes(@RequestBody Map<String,String> map) throws ParseException {
        String t_manage_templateId = map.get("t_Manage_templateId");
        String t_indeit_scode = map.get("t_indeit_scode");
        String t_inmoving_code = map.get("t_inmoving_code");
        String createDate = map.get("createDate");
        String endDate = map.get("endDate");
        String tmerber = map.get("tmerber");
        String name=map.get("name");
        Date parse = new SimpleDateFormat("yyyy-MM-dd").parse(createDate);
        Date parse1 = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);
        if(t_manage_templateId!=null){
            int i1 = tInterventionprogrammesService.DelteBytermer(tmerber);
            if(i1>0){
                TInterventionprogrammestable tInterventionprogrammestable=new TInterventionprogrammestable();
                tInterventionprogrammestable.setTInterveneMerber(tmerber);
                tInterventionprogrammestable.setCreatedate(parse);
                tInterventionprogrammestable.setEnddate(parse1);
                tInterventionprogrammestable.setTintername(name);
                tInterventionprogrammestable.setExecutionstatus(0);
                tInterventionprogrammestable.setTManageTemplateid(t_manage_templateId);
                int i = tInterventionprogrammesService.insertSelective(tInterventionprogrammestable);
                if(i>0){
                    return 1;
                }
            }

        }else{
            int i1 = tInterventionprogrammesService.DelteBytermer(tmerber);
            if(i1>0) {
                JSONArray jsonArray = JSONArray.fromObject(t_indeit_scode);
                String[] arr = new String[jsonArray.size()];
                for (int i = 0; i < jsonArray.size(); i++) {
                    arr[i] = jsonArray.getString(i);
                }

                JSONArray jsonArray2 = JSONArray.fromObject(t_inmoving_code);
                String[] arr2 = new String[jsonArray2.size()];
                for (int i = 0; i < jsonArray2.size(); i++) {
                    arr2[i] = jsonArray2.getString(i);
                }
                forInterventprogrammes forInterventprogrammes = new forInterventprogrammes();
                forInterventprogrammes.setTindeitscodes(arr);
                forInterventprogrammes.setTmerber(tmerber);
                forInterventprogrammes.setTinmovingcodes(arr2);
                forInterventprogrammes.setStauts(0);
                forInterventprogrammes.setName(name);
                forInterventprogrammes.setCreateDate(parse);
                forInterventprogrammes.setEndDate(parse1);
                int i = tInterventionprogrammesService.forInsert(forInterventprogrammes);
                if(i>0){
                    return 2;
                }
            }
        }
        return 0;
    }
    //做过计划的所有会员
    @PostMapping("/selectAllinterveneTmerber")
    @ResponseBody
    public PageInfo<TMember> selectAllinterveneTmerber(@RequestBody Map<String,String> map){
        String page = map.get("page");
        String num = map.get("num");
        page=page==null?"1":page;
        String content = map.get("content");
        content=content.equals("")?null:content;
        return tMemberService.selectAllinterveneTmerber(Integer.parseInt(page),Integer.parseInt(num),content);
    }
    //查询所有的随访记录
    @PostMapping("/selectAllTFollow")
    @ResponseBody
    public PageInfo<TFollowupjournal> selectAllTFollow(@RequestBody Map<String,String> map){
        String page = map.get("page");
        String num = map.get("num");
        page=page==null?"1":page;
        String content = map.get("content");
        content=content.equals("")?null:content;
        return tFollowupjournalService.selectAllTFollow(Integer.parseInt(page),Integer.parseInt(num),content);
    }
    //查询所有的痊愈随访记录
    @PostMapping("/selectAllTfollwBytfinsh")
    @ResponseBody
    public PageInfo<TFollowupjournal> selectAllTfollwBytfinsh(@RequestBody Map<String,String> map){
        String page = map.get("page");
        String num = map.get("num");
        page=page==null?"1":page;
        String content = map.get("content");
        content=content.equals("")?null:content;
        return tFollowupjournalService.selectAllTfollwBytfinsh(Integer.parseInt(page),Integer.parseInt(num),content);
    }
    //查询编号会员是否存在 干预的时间方案时间是否已过期
    @PostMapping("/selectIntervenByInqiereaBy")
    @ResponseBody
    public int selectIntervenByInqiereaBy(@RequestBody Map<String,String> map) throws ParseException {
        String name = map.get("name");
        String termer = map.get("termer");
        String date1 = map.get("Date1");
        Date parse = new SimpleDateFormat("yyyy-MM-dd").parse(date1);
        inquireaBy inquirea =new inquireaBy();
        inquirea.setName(name);
        inquirea.setTmerber(termer);
        inquirea.setFollwdate(parse);
        List<TInterventionprogrammestable> tInterventionprogrammestables = tInterventionprogrammesService.inquireaBytmetberName(inquirea);
        if(tInterventionprogrammestables.size()>0){
            List<TInterventionprogrammestable> tInterventionprogrammestables1 = tInterventionprogrammesService.inquireaBytmetberNameDate(inquirea);
            if(tInterventionprogrammestables1.size()>0){
                if(tInterventionprogrammestables1.get(0).getExecutionstatus()>0){
                    return  3;
                }
                return 2;
            }else{
                return 1;
            }
        }
        return 0;
    }
    //添加随访记录
    @PostMapping("/addfollw")
    @ResponseBody
    public int addfollw(@RequestBody Map<String,String> map) throws ParseException {
        String name = map.get("name");
        String termer = map.get("termer");
        String date1 = map.get("Date1");
        Date parse = new SimpleDateFormat("yyyy-MM-dd").parse(date1);
        String content = map.get("Content");
        String feedback = map.get("feedback");
        String manner = map.get("manner");
        String tresult = map.get("tresult");
        String confirmCaller = map.get("ConfirmCaller");
        TFollowupjournal tFollowupjournal=new TFollowupjournal();
        tFollowupjournal.setTFollowdate(parse);
        tFollowupjournal.setTResult(Integer.parseInt(tresult));
        if(Integer.parseInt(tresult)>0){
            tFollowupjournal.setTfinish(1);
            //改变所有得显示随访
            tFollowupjournalService.updatetfinish(termer);
            tMemberService.updateAll(termer);
        }else{
            tFollowupjournal.setTfinish(0);
        }
        tFollowupjournal.setTFollowname(name);
        tFollowupjournal.setTFollowtmerber(termer);
        tFollowupjournal.setTFollowcontent(content);
        tFollowupjournal.setTFollowfeedback(feedback);
        tFollowupjournal.setTManner(manner);
        tFollowupjournal.setTFollowconfirmcaller(confirmCaller);
        int i = tFollowupjournalService.insertSelective(tFollowupjournal);
        if(i>0){
            return 1;
        }
        return 0;
    }
    //修改 查询用户做的所有计划 模板
    @PostMapping("/inquireabouttmetber")
    @ResponseBody
    public  List<TInterventionprogrammestable> inquireabouttmetber(@RequestBody Map<String,String> map){
        return tInterventionprogrammesService.inquireabouttmetber(map.get("tmerber"));
    }
    //体质 心理删除查询 人群分类中是否含有本编号
    @PostMapping("/deleteSelectByTInterpose")
    @ResponseBody
    public int deleteSelectBy(@RequestBody Map<String,String> map){
        TInterposeClassify tInterposeClassify = tInterposeClassifyService.selectByTmerber(map.get("tmerber"));
        if(tInterposeClassify!=null){
            return 1;
        }
        return 0;
    }
    //删除体质与用户体质选择的所有信息
    @PostMapping("/deleteTcDlBytermer")
    @ResponseBody
    public int deleteTcBytmerber(@RequestBody Map<String,String> map){
        int tmerber = tBodyTypeService.deleteBYtmerber(map.get("tmerber"));
        int tmerber1 = tUserConstitutionTableService.deleteByTmerber(map.get("tmerber"));
        if(tmerber>0 && tmerber1>0){
            return 1;
        }
        return 0;
    }
    //删除心理表与用户心理选项的所有信息
    @PostMapping("/deleteTpDlBytermer")
    @ResponseBody
    public int deleteTpDlBytermer(@RequestBody Map<String,String> map){
        int tmerber = tMentalityResultService.deleteBytermer(map.get("tmerber"));
        int tmerber1 = tUserPsychologicalEvaluationService.deleteByTmerber(map.get("tmerber"));
        if(tmerber>0 && tmerber1>0){
            return 1;
        }
        return 0;
    }
    //删除随访记录的所有数据 清空干预方案
    @PostMapping("/deletefwDlBytermer")
    @ResponseBody
    public int deletefwDlBytermer(@RequestBody Map<String,String> map){
        int tmerber = tInterventionprogrammesService.DelteBytermer(map.get("tmerber"));
        int tmerber1 = tFollowupjournalService.deleteByTmerber(map.get("tmerber"));
        if(tmerber>0 && tmerber1>0){
            return 1;
        }
        return 0;
    }
    //删除模板与禁用模板查询是否已经使用
    @PostMapping("/ChaMangeIntervention")
    @ResponseBody
    public int ChaMangeIntervention(@RequestBody Map<String,String> map){
        System.out.println(map);
        List<TInterventionprogrammestable> termer = tInterventionprogrammesService.queryBytemplateId(map.get("tmerber"));
        if(termer.size()>0){
            return 1;
        }
        return 0;
    }
    //删除模板
    @PostMapping("/DeleteMangeByid")
    @ResponseBody
    public int DeleteMangeByid(@RequestBody Map<String,String> map){
        int tmerber = tTemplatemanageService.deleteByManageId(map.get("tmerber"));
        if(tmerber>0){
            return 1;
        }
        return 0;
    }
    //修改模板
    @PostMapping("/updateMangeByid")
    @ResponseBody
    public int updateMangeByid(@RequestBody Map<String,String> map){
        int termer = tTemplatemanageService.updateByManageId(map.get("tmerber"));
        if(termer>0){
            return 1;
        }
        return 0;
    }
    //开启模板
    @PostMapping("/updateMangeByid2")
    @ResponseBody
    public int updateMangeByid2(@RequestBody Map<String,String> map){
        int termer = tTemplatemanageService.updateByManageId2(map.get("tmerber"));
        if(termer>0){
            return 1;
        }
        return 0;
    }
    //刘成杰文件 知识库 饮食数据表
    @PostMapping("/selectAllTMeals")
    @ResponseBody
    public  PageInfo<TMeals> selectAllTMeals(@RequestBody Map<String,String> map){
        String page = map.get("page");
        String num = map.get("num");
        page=page==null?"1":page;
        String content = map.get("content");
        content=content.equals("")?null:content;
        return tMealsService.selectAllTMeals(Integer.parseInt(page),Integer.parseInt(num),content);
    }

    //膳食库 添加
    @GetMapping("/addTmeals")
    @ResponseBody
    public Integer addTmeals(String tmeal){
        Gson gson = new Gson();
        TMeals tMeals = gson.fromJson(tmeal, TMeals.class);
        TMeals lastbean = tMealsService.Lastbean();
        String code = lastbean.getCode();
        String newTmid="";
        String substring="";
        if(lastbean==null){
            newTmid="SP001";
        }else{
            substring = code.substring(2, code.length());
        }
        if(Integer.parseInt(substring)<10){
            newTmid= "SP00"+(Integer.parseInt(substring)+1);
        }
        if(Integer.parseInt(substring)>=10){
            newTmid= "SP0"+(Integer.parseInt(substring)+1);
        }
        if(Integer.parseInt(substring)>=100){
            newTmid= "SP"+(Integer.parseInt(substring)+1);
        }
        tMeals.setCode(newTmid);
        //System.out.println(tMeals+"膳食添加");
        tMealsService.insertSelective(tMeals);
        return 1;
    }

    //膳食库 修改
    @GetMapping("/updTmeals")
    @ResponseBody
    public Integer updTmeals(String tmeal){
        Gson gson = new Gson();
        TMeals tMeals = gson.fromJson(tmeal, TMeals.class);
        //System.out.println(tMeals+"膳食修改");
        tMealsService.updateByPrimaryKeySelective(tMeals);
        return 1;
    }

    //膳食库 单条删除
    @GetMapping("/delTmeals")
    @ResponseBody
    public Integer delTmeals(String id){
        System.out.println(Integer.valueOf(id)+"膳食删除");
        tMealsService.deleteByPrimaryKey(Integer.valueOf(id));
        return 1;
    }
    //膳食库种类
    @GetMapping("/getAll")
    @ResponseBody
    public List<TMealsType> getAll(){
       return tMealsService.findAll();
    }
    //查询名称是否存在
    @GetMapping("byCode")
    @ResponseBody
    public Integer byCode(String name){
        TMeals bycode = tMealsService.getBycode(name);
        if(bycode==null){
            return 1;
        }else{
            return 2;
        }
    }

    //刘成杰知识库 运动数据表
    @PostMapping("/selectAllTSportsItem")
    @ResponseBody
    public PageInfo<TSportsItemLibrary> selectAllTSportsItem(@RequestBody Map<String,String> map){
        String page = map.get("page");
        String num = map.get("num");
        page=page==null?"1":page;
        String content = map.get("content");
        content=content.equals("")?null:content;
        return tSportsItemLibraryService.selectAllTSports(Integer.parseInt(page),Integer.parseInt(num),content);
    }

    //运动库 添加
    @GetMapping("/addTSport")
    @ResponseBody
    public Integer addTSport(String tsport){
        Gson gson = new Gson();
        TSportsItemLibrary tSport = gson.fromJson(tsport, TSportsItemLibrary.class);

        TSportsItemLibrary lastbean = tSportsItemLibraryService.Lastbean();
        String code = lastbean.getCode();
        String newTmid="";
        String substring="";
        if(lastbean==null){
            newTmid="YD001";
        }else{
            substring = code.substring(2, code.length());
        }
        if(Integer.parseInt(substring)<10){
            newTmid= "YD00"+(Integer.parseInt(substring)+1);
        }
        if(Integer.parseInt(substring)>=10){
            newTmid= "YD0"+(Integer.parseInt(substring)+1);
        }
        if(Integer.parseInt(substring)>=100){
            newTmid= "YD"+(Integer.parseInt(substring)+1);
        }
        tSport.setCode(newTmid);
        System.out.println(tSport+"运动添加");
        tSportsItemLibraryService.insertSelective(tSport);
        return 1;
    }

    //运动库 修改
    @GetMapping("/updTSport")
    @ResponseBody
    public Integer updTSport(String tsport){
        Gson gson = new Gson();
        TSportsItemLibrary tSport = gson.fromJson(tsport, TSportsItemLibrary.class);
        System.out.println(tSport+"运动修改");
        tSportsItemLibraryService.updateByPrimaryKeySelective(tSport);
        return 1;
    }

    //运动库 单条删除
    @GetMapping("/delTSport")
    @ResponseBody
    public Integer delTSport(String id){
        System.out.println(Integer.valueOf(id)+"运动删除");
        tSportsItemLibraryService.deleteByPrimaryKey(Integer.valueOf(id));
        return 1;
    }

    @PostMapping("/selectThByname")
    @ResponseBody
    public THealthmgr selectThByname(@RequestBody Map<String,String> map){
        THealthmgr tRole = TuserMapper.selectByName(map.get("name"));
        return  tRole;
    }
    @PostMapping("/selectByJiangKan")
    @ResponseBody
    public PageInfo<TMember> selectByJiangKan(@RequestBody Map<String,String> map){
        System.out.println(map);
        String page = map.get("page");
        String num = map.get("num");
        String janID = map.get("janID");
        String merber = map.get("content");
        jiangKangPojo jiangKangPojo = new jiangKangPojo();
        jiangKangPojo.setJanID(Integer.parseInt(janID));
        jiangKangPojo.setMerber(merber);
        return  TuserMapper.selectAllTmerber1(jiangKangPojo,Integer.parseInt(page),Integer.parseInt(num));
    }

    @PostMapping("/selectAllTFollow1")
    @ResponseBody
    public PageInfo<TFollowupjournal> selectAllTFollow1(@RequestBody Map<String,String> map){
        System.out.println(map);
        String page = map.get("page");
        String num = map.get("num");
        String janID = map.get("janID");
        String merber = map.get("content");
        jiangKangPojo jiangKangPojo = new jiangKangPojo();
        jiangKangPojo.setJanID(Integer.parseInt(janID));
        jiangKangPojo.setMerber(merber);
        return  TuserMapper.selectAllTFollow1(jiangKangPojo,Integer.parseInt(page),Integer.parseInt(num));
    }

    @PostMapping("/selectAllTFollow2")
    @ResponseBody
    public PageInfo<TFollowupjournal> selectAllTFollow2(@RequestBody Map<String,String> map){
        System.out.println(map);
        String page = map.get("page");
        String num = map.get("num");
        String janID = map.get("janID");
        String merber = map.get("content");
        jiangKangPojo jiangKangPojo = new jiangKangPojo();
        jiangKangPojo.setJanID(Integer.parseInt(janID));
        jiangKangPojo.setMerber(merber);
        return  TuserMapper.selectAllTFollow2(jiangKangPojo,Integer.parseInt(page),Integer.parseInt(num));
    }
}
